package com.atguigu.edu.controller;


import com.atguigu.commonUtils.R;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.vo.CourseInfoVo;
import com.atguigu.edu.entity.vo.CoursePublishVo;
import com.atguigu.edu.entity.vo.CourseQuery;
import com.atguigu.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/edu/course")
//@CrossOrigin //解决跨域问题
public class CourseController {

    @Autowired
    CourseService courseService;

    /**
     * 获取课程的列表
     *
     * @return
     */
    @GetMapping("getCourseList")
    public R getCourseList() {
        List<Course> list = courseService.list(null);
        return R.ok().data("list", list);
    }

    /**
     * 分页查询课程列表
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页查询课程列表")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit) {
        Page<Course> pageParam = new Page<>(page, limit);


        Page<Course> coursePage = courseService.page(pageParam);
        List<Course> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 带分页的条件查询 课程列表
     *
     * @param current
     * @param limit
     * @param courseQuery
     * @return
     */
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@ApiParam(value = "当前页码", required = true) @PathVariable long current,
                                 @ApiParam(value = "每页记录数", required = true) @PathVariable long limit,
                                 @ApiParam(value = "课程列表查询对象", required = false) @RequestBody CourseQuery courseQuery
    ) {
        Page<Course> coursePage = courseService.pageCourseCondition(current, limit, courseQuery);

        long total = coursePage.getTotal(); //总记录数
        List<Course> records = coursePage.getRecords();//数据list集合

        return R.ok().data("total", total).data("records", records);
    }

    /**
     * 添加课程基本信息
     *
     * @param courseInfoVo
     * @return
     */
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        //返回添加之后的课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    /**
     * 根据课程ID查询课程的基本信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);

        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    /**
     * 修改课程的信息
     *
     * @param courseInfoVo
     * @return
     */
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    /**
     * 根据课程ID查询课程的确认信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(courseId);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    /**
     * 课程的最终发布  修改课程的状态
     *
     * @param courseId
     * @return
     */
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId) {
        Course course = new Course();
        course.setId(courseId);
        course.setStatus("Normal"); //设置课程的发布状态
        courseService.updateById(course);
        return R.ok();
    }


    /**
     * 根据课程ID 删除课程
     *
     * @param courseId
     * @return
     */
    @DeleteMapping("/removeCourseById/{courseId}")
    public R removeCourseById(@PathVariable String courseId) {
        R result = courseService.removeCourseById(courseId);
        return R.ok();
    }
}

